package zhai.docking.alipay.domain.base.alipay.common;

import com.alipay.api.AlipayResponse;
import lombok.Data;
import zhai.docking.alipay.constant.AlipayConstant;
import zhai.docking.alipay.constant.StringConstant;
import zhai.docking.alipay.util.StringUtil;

/**
 * @author Created by 翟凤玺
 * @package zhai.docking.alipay.domain.base.alipay.common
 * @project-name zhai-docking-alipay
 * @description 基础 支付宝 公共 出参
 * @create-datetime 2020/12/31 11:12
 */
@Data
public class BaseAlipayCommonOut {

    /**
     * 返回码
     */
    private String code;

    /**
     * 返回码描述
     */
    private String msg;

    public boolean isSuccess() {
        return this.code.equals(AlipayConstant.RESULT_SUCCESS_CODE);
    }

    public BaseAlipayCommonOut commonOut(AlipayResponse response) {
        BaseAlipayCommonOut out = new BaseAlipayCommonOut();
        if (!AlipayConstant.CODE_SUCCESS.equals(response.getCode()) && StringUtil.isBlank(response.getSubCode())) {
            out.setCode(AlipayConstant.RESULT_ERROR_DEF_CODE);
            out.setMsg(StringUtil.join(response.getCode(), StringConstant.COLON, response.getMsg()));
        } else if (StringUtil.isNotBlank(response.getSubCode()) && !AlipayConstant.SUB_CODE_SUCCESS.equals(response.getSubCode())) {
            out.setCode(AlipayConstant.RESULT_ERROR_DEF_CODE);
            out.setMsg(StringUtil.join(response.getSubCode(), StringConstant.COLON, response.getSubMsg()));
        } else {
            out.setCode(AlipayConstant.RESULT_SUCCESS_CODE);
        }
        return out;
    }
}